package cn.enilu.guns.admin.modular.thread;

import cn.enilu.guns.admin.equipment.service.EntranceGuardService;
import cn.enilu.guns.admin.equipment.transform.GetDeviceDataTransform;
import cn.enilu.guns.bean.dto.RealTimeRecordDto;
import cn.enilu.guns.bean.entity.door.EquipmentInfo;
import cn.enilu.guns.service.system.impl.ConstantFactory;
import cn.enilu.guns.utils.Maps;
import cn.enilu.guns.utils.SpringContextUtil;

import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class UserCardReadThread extends Thread {
    private static Lock lock = new ReentrantLock();
    private static EntranceGuardService entranceGuardService = SpringContextUtil.getBean("entranceGuardService");
    public static long startTime = System.currentTimeMillis();
    private static int hCommPro;
    private static boolean startFlag = true;
    /**
     * 返回的实时数据队列
     */
    private static Queue<RealTimeRecordDto> realTimeRecordDtoQueue = new ConcurrentLinkedQueue<>();
    /**
     * 返回的实时数据队列
     */
    private static RealTimeRecordDto realTimeRecordDto = null;


    //静态内部类
    private static class UserCardReadSingletonHandler {
        private final static UserCardReadThread instance = new UserCardReadThread();
    }

    public static UserCardReadThread getInstance() {
        return UserCardReadSingletonHandler.instance;
    }

    private UserCardReadThread(){

    }

    public static void startRead(String ipAddress){
        connectHCommPro(ipAddress);
        UserCardReadThread userCardReadThread = new UserCardReadThread();
        startFlag = true;
        userCardReadThread.start();
    }
    public static void stopRead(){
        startFlag = false;
    }
    @Override
    public void run() {
        try {
            if(!lock.tryLock()){
                return;
            }
            while (startFlag) {
                //测试模拟数据代码
//                Thread.sleep(1000L);
                long endTime = System.currentTimeMillis();
                if (endTime - startTime > 30 * 1000) {
                    System.out.println("超过30秒没有人拉取实时数据，停止读取设备数据线程");
                    return;
                }
                //获取真实数据
                getRtLog();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private void getRtLog(){
        String rtLog = entranceGuardService.getRTLog(hCommPro);
        if ("0".equals(rtLog)) {
            return;
        }
        RealTimeRecordDto realTimeRecordDtoLog = GetDeviceDataTransform.transformToRealTimeRecord(rtLog);
        realTimeRecordDto = realTimeRecordDtoLog;
//        realTimeRecordDtoQueue.add(realTimeRecordDto);
    }

    public static void main(String[] args) throws InterruptedException {
    }

    public static void resetStartTime() {
        startTime = System.currentTimeMillis();
    }

    private static Random random = new Random();
    /**
     * 連接設備
     */
    private static boolean connectHCommPro(String ipAddress){
        int hCommProNew = entranceGuardService.getHCommPro(ipAddress);
        //正式链接设备代码
        if(hCommProNew > 0){
            hCommPro = hCommProNew;
            return true;
        }
        return false;
    }
    /**
     * 获取实时监控结果集
     * @return
     */
    public static Queue<RealTimeRecordDto> getRealTimeRecordDtos(){
        return realTimeRecordDtoQueue;
    }
    /**
     * 获取实时监控结果集
     * @return
     */
    public static RealTimeRecordDto getRealTimeRecordDto(){
        RealTimeRecordDto realTimeRecordDtoNew = realTimeRecordDto;
        realTimeRecordDto = null;
        return realTimeRecordDtoNew;
    }
}
